import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { FirstPersonControls } from "three/examples/jsm/controls/FirstPersonControls";
import {
  PerspectiveCamera,
  Scene,
  WebGLRenderer,
  Mesh,
  BoxBufferGeometry,
  MeshBasicMaterial,
  TextureLoader,
  CubeTextureLoader,
  AxesHelper,
  AmbientLight,
  PointLight,
  CubeRefractionMapping,
} from "three";
import { PLYLoader } from "three/examples/jsm/loaders/PLYLoader";
import { MeshPhongMaterial } from "three";

let camera: PerspectiveCamera, scene: Scene, renderer: WebGLRenderer;
let orbitControl: OrbitControls;
let firstPersonControl: FirstPersonControls;
let mesh: Mesh;

init();
animate();

function init() {
  camera = new PerspectiveCamera(
    70,
    window.innerWidth / window.innerHeight,
    1,
    10000
  );
  camera.position.set(0, 0, -4000);

  scene = new Scene();

  // add light
  let ambientLight = new AmbientLight('#fff')
  let pointLight = new PointLight('#fff')
  // pointLight.position.set(0, 4000, 0)
  scene.add(ambientLight)
  scene.add(pointLight)

  //   set axes
  //   let axes = new AxesHelper(10000);
  //   scene.add(axes);

  // set background
  let cubeTextureLoader = new CubeTextureLoader();
  // 立方体盒子贴图顺序
  // [right,left,up,down,front,back]
  let imgs = [
    require("../static/textures/Park3Med/px.jpg").default,
    require("../static/textures/Park3Med/nx.jpg").default,
    require("../static/textures/Park3Med/py.jpg").default,
    require("../static/textures/Park3Med/ny.jpg").default,
    require("../static/textures/Park3Med/pz.jpg").default,
    require("../static/textures/Park3Med/nz.jpg").default,
  ];
  let textureCube = cubeTextureLoader.load(imgs);
  textureCube.mapping = CubeRefractionMapping;
  scene.background = textureCube;

  // load model
  let mLoader = new PLYLoader();
  mLoader.load(require('../static/modal/Lucy100k.ply').default, geometry => {
    geometry.computeVertexNormals();
    let material1 = new MeshPhongMaterial({
      color: '#0ff',
      envMap: textureCube,
      reflectivity: 1,  
      refractionRatio: .98
    })
    let mesh1 = new Mesh(geometry, material1)
    scene.add(mesh1)
  })

  renderer = new WebGLRenderer({ antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  orbitControl = new OrbitControls(camera, renderer.domElement);
  // orbitControl.maxDistance = 2000000;
  // orbitControl.minDistance = 10;
  //   firstPersonControl = new FirstPersonControls(camera, renderer.domElement);
  document.body.appendChild(renderer.domElement);
  // renderer.render(scene, camera);
}

function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
}
